<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests\CategoryRequest;
use App\Http\Controllers\Controller;
use App\Model\Category;
use DB,Input,Validator,Cache;

/**
 * 商品分类控制器
 */
class CategoryController extends Controller
{
    // 商品分类列表
    public function index(Request $request)
    {
//        DB::connection()->enableQueryLog();
        // 存储搜索条件
        $request->flashOnly('cate_name');
        
        $cates = Category::where('cate_status', '<>', '-1')
                ->where('cate_name', 'like', '%'. $request->get('cate_name') .'%')
                ->orderByRaw('concat(path,id)')
                ->orderBy('cate_sort','asc')
                ->paginate(15);
//                ->get()->toArray();
//        $cates = collect(Category::getList($cates));
//        print_r(DB::getQueryLog());
        return view('admin.category.index', compact('cates'));
    }
    
    // 分类添加模板
    public function create($pid=0)
    {
        $cate_name = DB::table('category')->where('id', $pid)->pluck('cate_name');
        if($pid != 0 && !$cate_name){
            return redirect('/admin/category')->with('errors', '分类不存在');
        }
        return view('admin.category.add', [ 'pid'=>$pid, 'cate_name'=>$cate_name]);
    }
    
    // 保存商品分类
    public function store(CategoryRequest $request)
    {
        $input = $request->except('_token');
        $pid = $input['pid'];
        if($pid != 0){
            $input['path'] = DB::table('category')->where('id', $pid)->pluck('path') . $pid . ',';
        }
        
        $result = DB::table('category')->insert($input);
        
        if ($result) {
            $this->cacheClean();
            return redirect('/admin/category');
        } else {
            return back()->with(['errors'=>'添加失败']);
        }
    }
    
    // 修改分类
    public function edit($id)
    {
        $category = DB::table('category');
        $cates = $category->get();
        $data = $category->where('id', $id)->first();
        return view('admin.category.edit', ['cates'=>$cates, 'data'=>$data]);
    }
    
    // 执行修改
    public function update(CategoryRequest $request,$id)
    {
        $input = $request->except(['_token','_method']);
        $result = DB::table('category')->where('id', $id)->update($input);
        if ($result) {
            $this->cacheClean();
            return redirect('/admin/category');
        } else {
            return back()->with(['errors'=>'修改失败']);
        }
    }
    
    public function show($id){
        
    }
    
    // 删除
    public function destroy($id)
    {
//        DB::connection()->enableQueryLog();
        // 判断是否存在子分类
        $children = DB::table('category')->where('pid', $id)->first();
        $r = ['status'=>0, 'info'=>''];
        if ( $children ) {
            $r['info'] = '存在子类，不能删除';
        } else {
            $res = DB::table('category')->where('id', $id)->update(['cate_status'=> -1]);
            
            if($res){
                $r['status'] = 1;
                $r['info'] = '删除成功';
                $this->cacheClean();
            } else {
                $r['info'] = '删除失败';
            }
        }
        return response()->json($r);
    }
    
    // 显示 隐藏
    public function showHidden(Request $request)
    {
        $r = ['status' => 0, 'info'=>''];
        
        // 获取子类id
        $ids = Category::getSonsId($request->get('id'));
        $ids = explode(',', $ids);
        $res = DB::table('category')->whereIn('id', $ids)->update(['cate_status'=>$request->get('status'),'cate_isnav'=>0]);
        if ($res) {
           $r['status'] = 1;
           $r['info'] = '修改成功';
           $this->cacheClean();
        } else {
            $r['info'] = '修改失败';
        }
        
        return response()->json($r);
    }
    
    // 显示在导航条
    public function isNav(Request $request)
    {
        $r = ['info'=>'设置失败','status'=>'0'];
        $cate = Category::where('id', $request->get('id'))->first();
        if($cate->cate_status != 1){
            $r['info'] = '隐藏的分类不能显示在导航条';
            return response()->json($r);
        }
        // 修改状态
        $cate->cate_isnav = $request->get('isnav');
        $res = $cate->save();
        
        if(false !== $res){
            $r['status'] = 1;
            $r['info'] = '设置成功';
            $this->cacheClean();
        }
        
        return response()->json($r);
    }
    
    public function cacheClean()
    {
        if(Cache::has('navs')){
            Cache::forget('navs');
        }
        if(Cache::has('navGoods')){
            Cache::forget('navs');
        }
    }
}